<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>接口文件生成</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        input,
        textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        button {
            background: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .post {
            border: 1px solid #ddd;
            padding: 15px;
            margin: 10px 0;
            border-radius: 5px;
        }
    </style>
</head>

<body onload="run()">
    <h1>AI 接口文件生成</h1>
    <!-- 创建文章表单 -->
    <form id="postForm">
        <div class="form-group">
            <label>URL:</label>
            <input type="text" name="url" required value="http://192.168.2.39:8080/v3/api-docs">
        </div>
        <button type="submit">生成</button>
    </form>

    <div>
        <a href="/download">
            <h1>下载</h1>
        </a>
    </div>

    <div class="result">
    </div>
    <script>

        function loadLastRecord() {
            const txt = localStorage.getItem('record') || '';
            if (txt) {
                document.querySelector('.result').innerHTML = txt;
            }
        }

        function run() {
            // 提交新文章
            document.getElementById('postForm').addEventListener('submit', async (e) => {
                e.preventDefault();

                const formData = new FormData(e.target);
                const data = {
                    url: formData.get('url'),
                };

                try {
                    const response = await fetch('/api/gen_code', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify(data)
                    });



                    if (response.ok) {
                        e.target.reset();
                        const now = new Date();
                        const txt = '生成成功: ' + `${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()} ${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}`;
                        localStorage.setItem('record', txt)
                        document.querySelector('.result').innerHTML = txt;
                    }
                } catch (error) {
                    console.error('发布失败:', error);
                }
            });

            loadLastRecord();
        }
    </script>
</body>

</html>